package com.zhou.dao.impl;

import com.zhou.dao.BaseAdminDao;
import com.zhou.utils.DbUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BaseAdminDaoIMpl extends DbUtils implements BaseAdminDao {

    /**
     * 根据用户名，更新登录时间和登录ip
     *
     * @param username
     * @param date
     * @param ip
     */
    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        String sql = "update eadmin set logintime=?,loginip=? where username=?";
        List param = new ArrayList();
        param.add(date);//有个小bug等下注意一下
        param.add(ip);
        param.add(username);
        try {
            update(sql, param);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();
        }
    }

    /**
     * 管理员根据账号密码登录
     *
     * @param username
     * @param password
     * @return 登录的结果 true表示成功
     */
    @Override
    public boolean login(String username, String password) {
        String sql = "select id from eadmin where username=? and password=?";
        List param = new ArrayList();
        param.add(username);
        param.add(password);
        ResultSet query = query(sql, param);
        try {
            return query.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return false;
    }
}
